<template>
	<view class="container">
		<view class="ui-all">
			<view class="avatar" @tap="avatarChoose">
				<view class="imgAvatar">
					<view class="iavatar" style="background: no-repeat center/cover #eeeeee;">
						<u-avatar class="to" :src="infoData.avatar" style="width: 100%; height: 100%;"></u-avatar>
					</view>
				</view>
				<text v-if="infoData.avatar">修改头像</text>
				<text v-if="!infoData.avatar">添加头像</text>
				<!-- <button v-if="!avater" open-type="getUserInfo" @getuserinfo="getUserInfo" class="getInfo"></button> -->
			</view>
			<view class="ui-list">
				<text>账号</text>
				<input v-model="infoData.account" type="text" placeholder-class="place" disabled />
			</view>
			<view class="ui-list">
				<text>昵称</text>
				<input v-model="infoData.nickname" type="text" placeholder="请输入昵称" placeholder-class="place" />
			</view>
			<view class="ui-list">
				<text>密码</text>
				<input v-model="infoData.password" type="password" placeholder="请输入密码" placeholder-class="place" @change="changepassword" />
			</view>
			<view class="ui-list" v-show="repeatflag">
				<text>再次输入密码</text>
				<input v-model="infoData.repeatpassword" type="password" placeholder="请输入密码" placeholder-class="place" />
			</view>
			<view class="ui-list">
				<text>手机号</text>
				<input v-model="infoData.phone" type="text" placeholder="请输入手机号" placeholder-class="place" />
			</view>
			<button class="save" @tap="savaInfo">保 存 修 改</button>
		</view>
		<u-toast ref="uToast" />
		<view>
			<button class="loginout" @tap="loginout">退出登录</button>
		</view>
	</view>
</template>

<script>
	import {
		mapMutations
	} from 'vuex'
	let token=uni.getStorageSync('token')
	export default {

		data() {
			return {
				repeatflag:false,
				myHeaders: {
					Authorization: token
				},
				show: false,
				infoData: {
					role:'',
					id:'',
					account: '',
					nickname: '',
					phone: '',
					avatar: '',
					password: '',
					repeatpassword:''
				},
				value: '请填写',
				sex: [{
					id: 1,
					name: '男'
				}, {
					id: 2,
					name: '女'
				}],
				index: 0,
				region: ['请选择'],
				date: '请填写',
				avater: '',
				description: '',
				url: '',
				nickName: '',
				mobile: '',
				headimg: '',
				taketable: {
					isadd: '1',
					name: '',
					userinfo: {
						account: '',
						personnum: 0
					},
				},

			}

		},
		created() {
			this.account = JSON.parse(uni.getStorageSync('currentUser')).account
			this.$u.get('/users/getuserbyaccount?account='+this.account).then(res=>{
				this.infoData.account=res.data.user.account
				this.infoData.nickname=res.data.user.nickname
				this.infoData.password=res.data.user.password
				this.infoData.avatar=res.data.user.avatar
				console.log(this.infoData.avatar,'wwew');
				// this.avater=this.infoData.avatar
				this.infoData.phone=res.data.user.phone
				this.infoData.id=res.data.user._id
				this.infoData.role=res.data.user.role
			})
		},
		methods: {
			changepassword(){
				this.repeatflag=true
			},
			...mapMutations(['CLEAN_DISHES', 'CLEAN_TABLE']),
			loginout() {
				if(uni.getStorageSync('vuexstate')){
					console.log(111)
					this.taketable.userinfo.account = JSON.parse(uni.getStorageSync('currentUser')).account
					this.taketable.isadd = "1"
					this.taketable.name = JSON.parse(uni.getStorageSync('vuexstate')).tabledetail.tablenumber
					this.taketable.userinfo.personnum = JSON.parse(uni.getStorageSync('vuexstate')).tabledetail.personnum
					console.log(this.taketable, 'sdv')
					// uni.removeStorageSync('vuexstate')
					// uni.removeStorageSync('tablerecords')
					// this.$u.post('/table/changetableuse', this.taketable).then(res => {
					// 	console.log(res, '取消桌位占用')
					// 	uni.removeStorageSync('currentUser')
					// 	uni.removeStorageSync('tablerecords')
					// 	uni.removeStorageSync('vuexstate')
					// 	uni.removeStorageSync('orderList')
					// 	uni.removeStorageSync('orderData')
					// 	this.CLEAN_DISHES()
					// 	this.CLEAN_TABLE()
					// })
				}else{
					console.log(222)
					uni.removeStorageSync('tablerecords')
					uni.removeStorageSync('vuexstate')
					uni.removeStorageSync('orderList')
					uni.removeStorageSync('orderData')
					this.CLEAN_TABLE()
					// this.CLEAN_DISHES()
					uni.removeStorageSync('currentUser')
					
				}
				

				// uni.navigateTo({
				// 	url: '',
				// 	animationType: 'pop-in',
				// 	animationDuration: 200
				// })
				this.$refs.uToast.show({
					title: '退出登录成功',
					type: 'success',
					duration: "200",
					url: '/pages/login/login'
				})
			},
			avatarChoose() {
				let that = this;
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					sourceType: ['album', 'camera'],
					success(res) {
						// tempFilePath可以作为img标签的src属性显示图片
						that.avater=res.tempFilePaths[0]
						that.infoData.avatar = res.tempFilePaths[0]
						console.log(that.infoData.avatar,'that.infoData.avatar');
						// that.imgUpload(res.tempFilePaths[0]);
						// const tempFilePaths = res.tempFilePaths;
					}
				});
			},
			getphonenumber(e) {
				if (e.detail.iv) {
					console.log(e.detail.iv) //传后台解密换取手机号
					uni.showToast({
						title: '已授权',
						icon: 'none',
						duration: 2000
					})
				}
			},
			savaInfo() {
				var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
				if(!myreg.test(this.infoData.phone)){
					return uni.showToast({
						title: '手机号格式不正确',
						icon: 'none',
						duration: 2000
					})
				}
				if((this.infoData.repeatpassword==this.infoData.password&this.repeatflag==true)||this.repeatflag==false){
					uni.showLoading({
						title:'保存中...'
					})
					this.imgUpload(this.avater)
				}else{
					uni.showToast({
						title: '两次密码不一致！',
						icon: 'none',
						duration: 2000
					})
				}
			},
			isPoneAvailable(poneInput) {
				var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
				if (!myreg.test(poneInput)) {
					return false;
				} else {
					return true;
				}
			},
			imgUpload(file) {
				let that = this;
				if(this.avater!=''){
					uni.uploadFile({
						url: '192.168.43.49:3000/uploadavatar', //需传后台图片上传接口
						filePath: file,
						name: 'file',
						header:this.myHeaders,
						formData: {
							id: this.infoData.id
						},
						success: function(res) {
							that.infoData.avatar = res.data
							console.log(that.infoData,'上传成功后的回掉');
							uni.hideLoading()
							that.$u.post('/users/updataUser', that.infoData).then(res => {
								if (res.data.code == 200) {
									let info=JSON.parse(uni.getStorageSync('currentUser'))
									info.avatar=that.infoData.avatar
									uni.setStorageSync('currentUser',JSON.stringify(info))
									uni.hideLoading()
									that.$refs.uToast.show({
										title: res.data.msg,
										type: 'success',
										duration: 200,
										isTab: true,
										url: 'pages/person/person'
									})
								}
							})
						},
						fail: function(error) {
							console.log(error);
						}
					});
				}else{
					that.$u.post('/users/updataUser', that.infoData).then(res => {
						if (res.data.code == 200) {
							let info=JSON.parse(uni.getStorageSync('currentUser'))
							info.avatar=that.infoData.avatar
							uni.setStorageSync('currentUser',JSON.stringify(info))
							that.$refs.uToast.show({
								title: res.data.msg,
								type: 'success',
								duration: 200,
								isTab: true,
								url: 'pages/person/person'
							})
						}
					})
				}
				
			},

		},
		onLoad() {}

	}
</script>

<style lang="scss">
	.loginout {
		position: absolute;
		bottom: 30rpx;
		left:15%;
		width: 70%;
		background: #ff0000;
		border: none;
		color: #ffffff;
		margin-top: 40rpx;
		font-size: 28rpx;
	}

	.container {
		display: block;
		height: 100%;

	}

	.ui-all {
		padding: 20rpx 40rpx;

		.avatar {
			width: 100%;
			text-align: left;
			padding: 20rpx 0;
			border-bottom: solid 1px #f2f2f2;
			position: relative;

			.imgAvatar {
				width: 140rpx;
				height: 140rpx;
				border-radius: 50%;
				display: inline-block;
				vertical-align: middle;
				overflow: hidden;

				.iavatar {
					width: 100%;
					height: 100%;
					display: block;
				}
			}

			text {
				display: inline-block;
				vertical-align: middle;
				color: #8e8e93;
				font-size: 28rpx;
				margin-left: 40rpx;
			}

			&:after {
				content: ' ';
				width: 20rpx;
				height: 20rpx;
				border-top: solid 1px #030303;
				border-right: solid 1px #030303;
				transform: rotate(45deg);
				-ms-transform: rotate(45deg);
				/* IE 9 */
				-moz-transform: rotate(45deg);
				/* Firefox */
				-webkit-transform: rotate(45deg);
				/* Safari 和 Chrome */
				-o-transform: rotate(45deg);
				position: absolute;
				top: 85rpx;
				right: 0;
			}
		}

		.ui-list {
			width: 100%;
			text-align: left;
			padding: 20rpx 0;
			border-bottom: solid 1px #f2f2f2;
			position: relative;

			text {
				color: #4a4a4a;
				font-size: 28rpx;
				display: inline-block;
				vertical-align: middle;
				min-width: 150rpx;
			}

			input {
				color: #030303;
				font-size: 30rpx;
				display: inline-block;
				vertical-align: middle;
			}

			button {
				color: #030303;
				font-size: 30rpx;
				display: inline-block;
				vertical-align: middle;
				background: none;
				margin: 0;
				padding: 0;

				&::after {
					display: none;
				}
			}

			picker {
				width: 90%;
				color: #030303;
				font-size: 30rpx;
				display: inline-block;
				vertical-align: middle;
				position: absolute;
				top: 30rpx;
				left: 150rpx;
			}

			u-picker {
				width: 90%;
				color: #030303;
				font-size: 30rpx;
				display: inline-block;
				vertical-align: middle;
				position: absolute;
				top: 30rpx;
				left: 150rpx;
			}

			textarea {
				color: #030303;
				font-size: 30rpx;
				vertical-align: middle;
				height: 150rpx;
				width: 100%;
				margin-top: 50rpx;
			}

			.place {
				color: #999999;
				font-size: 28rpx;
			}
		}

		.right:after {
			content: ' ';
			width: 20rpx;
			height: 20rpx;
			border-top: solid 1px #030303;
			border-right: solid 1px #030303;
			transform: rotate(45deg);
			-ms-transform: rotate(45deg);
			/* IE 9 */
			-moz-transform: rotate(45deg);
			/* Firefox */
			-webkit-transform: rotate(45deg);
			/* Safari 和 Chrome */
			-o-transform: rotate(45deg);
			position: absolute;
			top: 40rpx;
			right: 0;
		}

		.save {
			background: #ff5733;
			border: none;
			color: #ffffff;
			margin-top: 40rpx;
			font-size: 28rpx;
		}
	}
</style>
